﻿using SyncFileCommon.BLL;
using SyncFileCommon.Class;
using SyncFileCommon.Model;
using System.ComponentModel;

namespace SyncFileOption
{
    public partial class FrmClientOption : Form
    {

        private BindingList<SyncPlanModel> SyncPlanList;

        public FrmClientOption()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;//设置边框为不可调节
            this.MaximizeBox = false; //取消最大化按键
            this.MinimizeBox = false; //取消最小化按键
            this.ShowInTaskbar = false;//不在任务栏显示多个窗体框

        }

        private void FrmClientOption_Load(object sender, EventArgs e)
        {
            txtDeleteFileDirPath.Text = ConfigBLL.ClientConfig.DeleteFileDir.ToString();
            txtDeleteFileSaveDay.Text = ConfigBLL.ClientConfig.DeleteFileSaveDay.ToString();

            List<SyncPlanModel> _syncPlanList = SyncPlanBLL.SyncPlanListGet();
            if (_syncPlanList != null)
            {
                SyncPlanList = new BindingList<SyncPlanModel>(_syncPlanList);
            }
            else
            {
                SyncPlanList = new BindingList<SyncPlanModel>();
            }

            //设置禁止生成列
            gridPlan.MultiSelect = false;
            gridPlan.AutoGenerateColumns = false;
            gridPlan.AllowUserToAddRows = false;
            gridPlan.DataSource = SyncPlanList;

        }

        private void BtnSystem_Click(object sender, EventArgs e)
        {
            pnlMain.AutoScrollPosition = new Point(0, -1 * pnlMain.AutoScrollPosition.Y + pnlSystem.Top);

            pnlMain.Refresh();
            pnlMain.Invalidate();
            pnlMain.Update();
        }

        private void btnPlan_Click(object sender, EventArgs e)
        {
            pnlMain.AutoScrollPosition = new Point(0, -1 * pnlMain.AutoScrollPosition.Y + pnlPlan.Top);

            pnlMain.Refresh();
            pnlMain.Invalidate();
            pnlMain.Update();
        }

        private void btnClientSelectDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.SelectedPath = "";
            folderBrowser.Description = "请选择文件夹";
            folderBrowser.ShowNewFolderButton = true;//是否显示新建文件夹按钮
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                this.txtDeleteFileDirPath.Text = folderBrowser.SelectedPath;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //保存配置 
            ConfigBLL.ClientConfig.DeleteFileDir = txtDeleteFileDirPath.Text.Trim();
            ConfigBLL.ClientConfig.DeleteFileSaveDay = Convert.ToInt32(txtDeleteFileSaveDay.Text.Trim());

            List<SyncPlanModel> _syncPlanList = new List<SyncPlanModel>(SyncPlanList);
            SyncPlanBLL.SyncPlanListSave(_syncPlanList);

            ConfigBLL.SaveConfig();

            this.DialogResult = DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnAddPlan_Click(object sender, EventArgs e)
        {
            FrmSyncPlan _frmSyncPlan = new FrmSyncPlan();
            _frmSyncPlan.IsAdd = true;
            DialogResult _dr = _frmSyncPlan.ShowDialog(this);

            if (_dr == DialogResult.OK)
            {

                SyncPlanModel _syncPlan = new SyncPlanModel();
                _syncPlan.ID = CommonHelper.GetNewID();
                _syncPlan.ServerIP = _frmSyncPlan.ServerIP;
                _syncPlan.ServerPort = _frmSyncPlan.ServerPort;
                _syncPlan.UserName = _frmSyncPlan.UserName;
                _syncPlan.Password = _frmSyncPlan.Password;
                _syncPlan.DirPath = _frmSyncPlan.DirPath;
                _syncPlan.SaveDirPath = _frmSyncPlan.SaveDirPath;
                _syncPlan.RunType = _frmSyncPlan.RunType;
                _syncPlan.BeginTime = _frmSyncPlan.BeginTime;
                _syncPlan.EndTime = _frmSyncPlan.EndTime;
                _syncPlan.IntervalTime = _frmSyncPlan.IntervalTime;
                _syncPlan.State = _frmSyncPlan.State;
                SyncPlanList.Add(_syncPlan);
            }
            else
            {

            }
        }

        private void gridPlan_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 确认点击的是按钮列
            if (e.ColumnIndex == gridPlan.Columns["colUpdate"].Index && e.RowIndex >= 0)
            {
                // 执行按钮点击后的操作
                FrmSyncPlan _frmSyncPlan = new FrmSyncPlan();
                _frmSyncPlan.IsAdd = false;
                SyncPlanModel _syncPlan = SyncPlanList.First(it => it.ID == Convert.ToInt64(gridPlan.Rows[e.RowIndex].Cells["colID"].Value.ToString()));
                _frmSyncPlan.ID = _syncPlan.ID;
                _frmSyncPlan.ServerIP = _syncPlan.ServerIP;
                _frmSyncPlan.ServerPort = _syncPlan.ServerPort;
                _frmSyncPlan.UserName = _syncPlan.UserName;
                _frmSyncPlan.Password = _syncPlan.Password;
                _frmSyncPlan.DirPath = _syncPlan.DirPath;
                _frmSyncPlan.SaveDirPath = _syncPlan.SaveDirPath;
                _frmSyncPlan.RunType = _syncPlan.RunType;
                _frmSyncPlan.BeginTime = _syncPlan.BeginTime;
                _frmSyncPlan.EndTime = _syncPlan.EndTime;
                _frmSyncPlan.IntervalTime = _syncPlan.IntervalTime;
                _frmSyncPlan.State = _syncPlan.State;

                DialogResult _dr = _frmSyncPlan.ShowDialog(this);

                if (_dr == DialogResult.OK)
                {
                    _syncPlan.ServerIP = _frmSyncPlan.ServerIP;
                    _syncPlan.ServerPort = _frmSyncPlan.ServerPort;
                    _syncPlan.UserName = _frmSyncPlan.UserName;
                    _syncPlan.Password = _frmSyncPlan.Password;
                    _syncPlan.DirPath = _frmSyncPlan.DirPath;
                    _syncPlan.SaveDirPath = _frmSyncPlan.SaveDirPath;
                    _syncPlan.RunType = _frmSyncPlan.RunType;
                    _syncPlan.BeginTime = _frmSyncPlan.BeginTime;
                    _syncPlan.EndTime = _frmSyncPlan.EndTime;
                    _syncPlan.LastRunTime = DateTime.MinValue;
                    _syncPlan.IntervalTime = _frmSyncPlan.IntervalTime;
                    _syncPlan.State = _frmSyncPlan.State;
                }
                else
                {

                }

            }

            if (e.ColumnIndex == gridPlan.Columns["colDel"].Index && e.RowIndex >= 0)
            {
                // 执行按钮点击后的操作 
                string _serverIP = gridPlan.Rows[e.RowIndex].Cells["colServerIP"].Value.ToString();
                string _dirPath = gridPlan.Rows[e.RowIndex].Cells["colDirPath"].Value.ToString();

                DialogResult result = MessageBox.Show(this, "确认删除同步计划[ 服务器:" + _serverIP + ",文件夹:" + _dirPath + " ]吗？", "提示",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    // 用户选择了"是"，执行删除操作    
                    SyncPlanList.Remove(SyncPlanList.FirstOrDefault(m => m.ID == Convert.ToInt64(gridPlan.Rows[e.RowIndex].Cells["colID"].Value.ToString())));
                }
                else
                {
                    // 用户选择了"否"  
                }

            }
        }
    }
}
